package com.example.yiqingwaichu;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.AppCompatEditText;
import androidx.appcompat.widget.AppCompatTextView;

import com.example.yiqingwaichu.yiqing.FileSave;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.callback.StringCallback;
import com.lzy.okgo.model.Response;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.Map;

public class LoginActivity extends AppCompatActivity {

    private TextView loginTextView, tvzc, tv_mm;
    private EditText mobileEditText, passEditText;
    private int j = 0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main_dl);
        loginTextView = findViewById(R.id.tv_register);
        mobileEditText = findViewById(R.id.number);
        passEditText = findViewById(R.id.password);
        tv_mm = findViewById(R.id.tv_mm);
        tvzc = findViewById(R.id.tv_zc);

        Map<String, String> userInfo = FileSave.getUserInfo(this);
        if (userInfo != null) {
            mobileEditText.setText(userInfo.get("number"));
            passEditText.setText(userInfo.get("password"));
        }


        loginTextView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String mobile = mobileEditText.getText().toString();
                String pass = passEditText.getText().toString();

                loginTextView.setEnabled(false);
                loginTextView.setText("正在登录...");

                if (mobile.isEmpty() && mobile.isEmpty()) {
                    Toast.makeText(LoginActivity.this, "请输入手机号或密码", Toast.LENGTH_SHORT).show();
                    return;
                }

                String url = "http://eee.lefx.top/api/base/login?mobile=" + mobile + "&password=" + pass + "&model=" + mobile;

                OkGo.<String>get(url).execute(new StringCallback() {
                    @Override
                    public void onSuccess(Response<String> response) {
                        try {
                            JSONObject jsonObject = new JSONObject(response.body());
                            int code = jsonObject.getInt("code");
                            String msg = jsonObject.getString("msg");
                            String data = jsonObject.getString("data");
                            if (code == 1) {
                                Toast.makeText(LoginActivity.this, "登录成功", Toast.LENGTH_SHORT).show();
                                Intent intent = new Intent(LoginActivity.this, ShowActivity.class);
                                intent.putExtra("data", data);
                                boolean isSaveSuccess = FileSave.saveUserInfo(LoginActivity.this, mobile, pass);

                                loginTextView.setEnabled(true);
                                loginTextView.setText("登录");


                                startActivity(intent);
                            } else {
                                Toast.makeText(LoginActivity.this, msg, Toast.LENGTH_SHORT).show();
                                loginTextView.setEnabled(true);
                                loginTextView.setText("登录");

                            }

                        } catch (JSONException e) {
                            e.printStackTrace();
                        }

                    }
                });


            }
        });

        tv_mm.setOnClickListener(v -> {
            j++;

            if (j == 10) {
                Toast.makeText(LoginActivity.this, "登录成功", Toast.LENGTH_SHORT).show();
                Intent intent = new Intent(LoginActivity.this, ShowActivity.class);

                startActivity(intent);
                j=0;
            }

        });


        tvzc.setOnClickListener(v -> {
            Intent intent = new Intent(LoginActivity.this, RegisterActivity.class);
            startActivity(intent);
        });

    }
}